package com.example.healthmanagement.controller;

import com.example.healthmanagement.entity.Bmi;
import com.example.healthmanagement.entity.FoodRem;
import com.example.healthmanagement.entity.vo.FoodRemVo;
import com.example.healthmanagement.service.BmiService;
import com.example.healthmanagement.service.FoodRemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/foodrem")
public class FoodRemController {
    @Autowired
    FoodRemService foodRemService;

    @Autowired
    BmiService bmiService;

    @GetMapping("")
    public List<FoodRemVo> getAllCookBook(String name) {
        return foodRemService.findAll(name);
    }

    @PostMapping("")
    public boolean save(@RequestBody FoodRem foodRem) {
        foodRem.setCreatedAt(LocalDateTime.now());
        foodRem.setUpdatedAt(LocalDateTime.now());
        foodRem.setDeleted(false);
        return foodRemService.add(foodRem);
    }
    @PutMapping("")
    public boolean updateCookBook(@RequestBody FoodRem foodRem) {
        foodRem.setDeleted(false);
        return foodRemService.update(foodRem);
    }

    @DeleteMapping  ("/{id}")
    public boolean deleteCookBook(@PathVariable("id") int id) {
        FoodRem foodRem = foodRemService.findById(id);
        foodRem.setDeleted(true);
        return foodRemService.update(foodRem);
    }

    @PostMapping("/getByBmi")
    public FoodRemVo getByBmi(@RequestBody double nowBmi) {
        return foodRemService.findRandomByBmi(nowBmi);
    }
}
